#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include "album.h"
#include "png.h"
#include "bmp.h"
#include "jpeg.h"

extern int *lcd_ptr;

// 全局变量
static char (*image_names)[1024] = NULL;
static int image_count = 0;
static int current_page = 0;
static int images_per_page = 6;
static int full_screen_flag = 0;
static int img_idx = 0;

// 清屏函数
static void clear_screen(int color) {
    for (int i = 0; i < 800 * 480; i++) {
        lcd_ptr[i] = color;
    }
}

// 显示图片函数
static void display_image(const char *path, int x, int y, int width, int height) {
    char *ext = strrchr(path, '.');
    if (!ext) return;
    
    if (strcmp(ext, ".png") == 0) {
        lcd_draw_scale_png(path, x, y, width, height);
    } else if (strcmp(ext, ".bmp") == 0) {
        lcd_draw_scale_bmp(path, x, y, width, height);
    } else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0) {
        lcd_draw_jpeg(x, y, path);
    }
}

// 获取图片文件列表
int get_image_files(char *files[], int max_files) {
    DIR *dir = opendir("/zdata/album");
    if (!dir) return 0;
    
    struct dirent *entry;
    int count = 0;
    
    while ((entry = readdir(dir)) != NULL && count < max_files) {
        char *name = entry->d_name;
        char *ext = strrchr(name, '.');
        
        if (ext) {
            char lower_ext[10];
            strcpy(lower_ext, ext);
            for (int i = 0; lower_ext[i]; i++) {
                lower_ext[i] = tolower(lower_ext[i]);
            }
            
            if (strcmp(lower_ext, ".bmp") == 0 ||
                strcmp(lower_ext, ".jpg") == 0 ||
                strcmp(lower_ext, ".jpeg") == 0 ||
                strcmp(lower_ext, ".png") == 0) {
                
                char full_path[256];
                snprintf(full_path, sizeof(full_path), "/zdata/album/%s", name);
                files[count] = strdup(full_path);
                count++;
            }
        }
    }
    
    closedir(dir);
    return count;
}

// 显示相册界面
int show_album_ui(void) {
    if (!image_names) {
        image_names = calloc(512, 1024);
        char *files[512];
        image_count = get_image_files(files, 512);
        
        for (int i = 0; i < image_count; i++) {
            strcpy(image_names[i], files[i]);
            free(files[i]);
        }
    }
    
    clear_screen(0x000000);
    
    int start_idx = current_page * images_per_page;
    int end_idx = start_idx + images_per_page;
    if (end_idx > image_count) end_idx = image_count;
    
    // 显示图片缩略图
    for (int i = start_idx; i < end_idx; i++) {
        int row = (i - start_idx) / 3;
        int col = (i - start_idx) % 3;
        int x = 50 + col * 250;
        int y = 50 + row * 200;
        display_image(image_names[i], x, y, 200, 150);
    }
    
    return 0;
}

// 处理相册触摸事件
int handle_album_touch(int x, int y) {
    // 翻页按钮
    if (y < 100 && !full_screen_flag) {
        if (x < 100 && current_page > 0) {
            current_page--;
            show_album_ui();
            return 1;
        }
        if (x > 700) {
            int max_page = (image_count + images_per_page - 1) / images_per_page;
            if (current_page < max_page - 1) {
                current_page++;
                show_album_ui();
                return 1;
            }
        }
    }
    
    // 图片选择区域
    if (!full_screen_flag) {
        int start_idx = current_page * images_per_page;
        for (int i = 0; i < images_per_page; i++) {
            int row = i / 3;
            int col = i % 3;
            int img_x = 50 + col * 250;
            int img_y = 50 + row * 200;
            
            if (x >= img_x && x < img_x + 200 && y >= img_y && y < img_y + 150) {
                img_idx = start_idx + i;
                if (img_idx < image_count) {
                    clear_screen(0x000000);
                    display_image(image_names[img_idx], 160, 0, 480, 480);
                    full_screen_flag = 1;
                }
                return 1;
            }
        }
    }
    
    // 全屏模式控制
    if (full_screen_flag) {
        if (x > 0 && x < 100 && y >= 140 && y < 340) {
            // 上一张
            img_idx = (img_idx > 0) ? img_idx - 1 : image_count - 1;
            display_image(image_names[img_idx], 160, 0, 480, 480);
            return 1;
        }
        if (x > 700 && x < 800 && y >= 140 && y < 340) {
            // 下一张
            img_idx = (img_idx < image_count - 1) ? img_idx + 1 : 0;
            display_image(image_names[img_idx], 160, 0, 480, 480);
            return 1;
        }
        if (x > 700 && x < 800 && y >= 0 && y < 100) {
            // 退出全屏
            show_album_ui();
            full_screen_flag = 0;
            return 1;
        }
    }
    
    return 0;
}